home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / Kibitz⁄THINK C / Setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  6.6 KB  |  240 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     Kibitz
  5. ** File:        setup.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  19. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  21.  
  22. #ifndef __RESOURCES__
  23. #include <Resources.h>
  24. #endif
  25.  
  26. #ifndef __TOOLUTILS__
  27. #include <ToolUtils.h>
  28. #endif
  29.  
  30.  
  31.  
  32. /*****************************************************************************/
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. #pragma segment Config
  38. void    DoArrangeBoard(FileRecHndl frHndl, Point clickLoc)
  39. {
  40.     short            piece, fromSq, fromRow, fromCol, toRow, toCol, toSq;
  41.     short            palettePiece, color, delta, middle;
  42.     Rect            paletteRect, boardRect, squareRect;
  43.     Point            releaseLoc;
  44.     long            moveAmount;
  45.     RgnHandle        dragRgn;
  46.     ControlHandle    ctl;
  47.  
  48.     paletteRect = PaletteRect();
  49.     if (PtInRect(clickLoc, &paletteRect)) {
  50.         palettePiece = 1 + (clickLoc.h - paletteRect.left) / kBoardSqSize;
  51.         if (clickLoc.v < paletteRect.top + kBoardSqSize)
  52.             palettePiece = -palettePiece;
  53.         (*frHndl)->doc.palettePiece = palettePiece;
  54.         DrawPalette(frHndl);
  55.         SetOrigin(0, 0);
  56.         return;
  57.     }
  58.  
  59.     SetOrigin(0, 0);
  60.     clickLoc.h += 4096;
  61.  
  62.     boardRect = BoardRect();
  63.     if (PtInRect(clickLoc, &boardRect)) {
  64.  
  65.         fromRow = (clickLoc.v - kBoardVOffset) / kBoardSqSize;
  66.         fromCol = (clickLoc.h - kBoardHOffset) / kBoardSqSize;
  67.  
  68.         squareRect.top    = kBoardVOffset + fromRow * kBoardSqSize;
  69.         squareRect.left   = kBoardHOffset + fromCol * kBoardSqSize;
  70.         squareRect.bottom = squareRect.top  + kBoardSqSize + 2;
  71.         squareRect.right  = squareRect.left + kBoardSqSize + 2;
  72.  
  73.         if ((*frHndl)->doc.invertBoard) {
  74.             fromRow = 7 - fromRow;
  75.             fromCol = 7 - fromCol;
  76.         }
  77.         fromSq = START_IBNDS + 10 * fromRow + fromCol;
  78.  
  79.         if ((*frHndl)->doc.theBoard[fromSq] == EMPTY) {
  80.             piece = (*frHndl)->doc.palettePiece;
  81.             if (piece == WP) {
  82.                 if (fromRow == 7) return;
  83.                 if (fromRow == 0) piece = WQ;
  84.             }
  85.             if (piece == BP) {
  86.                 if (fromRow == 0) return;
  87.                 if (fromRow == 7) piece = BQ;
  88.             }
  89.             (*frHndl)->doc.theBoard[fromSq]  = piece;
  90.             (*frHndl)->doc.gameIndex      = 0;
  91.             (*frHndl)->doc.numGameMoves   = 0;
  92.             (*frHndl)->fileState.docDirty = true;
  93.             ctl = (*frHndl)->doc.gameSlider;
  94.             (*ctl)->contrlMax   = 0;
  95.             (*ctl)->contrlValue = 0;
  96.  
  97.             if (fromSq == 21) (*frHndl)->doc.king[BLACK].rookMoves[QSIDE] = 0;
  98.             if (fromSq == 28) (*frHndl)->doc.king[BLACK].rookMoves[KSIDE] = 0;
  99.             if (fromSq == 91) (*frHndl)->doc.king[WHITE].rookMoves[QSIDE] = 0;
  100.             if (fromSq == 98) (*frHndl)->doc.king[WHITE].rookMoves[KSIDE] = 0;
  101.                 /* Adjust castling privileges. */
  102.  
  103.             (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  104.             (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  105.                 /* Double-pawn-push must be last change to enable en-passant. */
  106.  
  107.             ImageDocument(frHndl, true);
  108.             return;
  109.         }
  110.  
  111.         dragRgn = NewRgn();
  112.         RectRgn(dragRgn, &squareRect);
  113.         moveAmount = DragGrayRgn(dragRgn, clickLoc, &boardRect, &boardRect,
  114.                                  noConstraint, nil);
  115.         DisposeRgn(dragRgn);
  116.  
  117.         if (moveAmount == 0x80008000L) return;
  118.  
  119.         releaseLoc.h = clickLoc.h + (moveAmount & 0xFFFF);
  120.         moveAmount   = moveAmount >> 16;
  121.         releaseLoc.v = clickLoc.v + (moveAmount & 0xFFFF);
  122.  
  123.         toRow = (releaseLoc.v - kBoardVOffset) / kBoardSqSize;
  124.         toCol = (releaseLoc.h - kBoardHOffset) / kBoardSqSize;
  125.         if ((*frHndl)->doc.invertBoard) {
  126.             toRow = 7 - toRow;
  127.             toCol = 7 - toCol;
  128.         }
  129.         toSq = START_IBNDS + 10 * toRow + toCol;
  130.  
  131.         if ((*frHndl)->doc.theBoard[toSq] == BK) return;
  132.         if ((*frHndl)->doc.theBoard[toSq] == WK) return;
  133.  
  134.         piece = (*frHndl)->doc.theBoard[fromSq];
  135.         if ((piece == WP) && (toSq >= 91)) return;
  136.         if ((piece == WP) && (toSq <= 28)) piece = WQ;
  137.         if ((piece == BP) && (toSq <= 28)) return;
  138.         if ((piece == BP) && (toSq >= 91)) piece = BQ;
  139.  
  140.         (*frHndl)->doc.theBoard[toSq]    = piece;
  141.         (*frHndl)->doc.theBoard[fromSq]  = EMPTY;
  142.         (*frHndl)->doc.gameIndex      = 0;
  143.         (*frHndl)->doc.numGameMoves   = 0;
  144.         (*frHndl)->fileState.docDirty = true;
  145.         ctl = (*frHndl)->doc.gameSlider;
  146.         (*ctl)->contrlMax   = 0;
  147.         (*ctl)->contrlValue = 0;
  148.  
  149.         if ((piece == WK) || (piece == BK)) {
  150.             color = (piece < 0) ? WHITE : BLACK;
  151.             (*frHndl)->doc.king[color].kingLoc   = toSq;
  152.             (*frHndl)->doc.king[color].kingMoves = true;
  153.         }
  154.  
  155.         if ((fromSq==21) || (toSq==21)) ++(*frHndl)->doc.king[BLACK].rookMoves[QSIDE];
  156.         if ((fromSq==28) || (toSq==28)) ++(*frHndl)->doc.king[BLACK].rookMoves[KSIDE];
  157.         if ((fromSq==91) || (toSq==91)) ++(*frHndl)->doc.king[WHITE].rookMoves[QSIDE];
  158.         if ((fromSq==98) || (toSq==98)) ++(*frHndl)->doc.king[WHITE].rookMoves[KSIDE];
  159.             /* Adjust castling privileges. */
  160.  
  161.         (*frHndl)->doc.enPasMove    = (*frHndl)->doc.arngEnPasMove    = 0;
  162.         (*frHndl)->doc.enPasPawnLoc = (*frHndl)->doc.arngEnPasPawnLoc = 0;
  163.             /* Double-pawn-push must be last change to enable en-passant. */
  164.  
  165.         delta = 0;
  166.         if (piece == WP)
  167.             if ((fromSq >= 81) && (fromSq <= 88))
  168.                 delta = fromSq - toSq;
  169.         if (piece == BP)
  170.             if ((fromSq >= 31) && (fromSq <= 38))
  171.                 delta = toSq - fromSq;
  172.         if (delta == 20) {
  173.             middle = (fromSq + toSq) / 2;
  174.             if (!(*frHndl)->doc.theBoard[middle]) {
  175.                 (*frHndl)->doc.enPasMove        = middle;
  176.                 (*frHndl)->doc.arngEnPasMove    = middle;
  177.                 (*frHndl)->doc.enPasPawnLoc     = toSq;
  178.                 (*frHndl)->doc.arngEnPasPawnLoc = toSq;
  179.             }
  180.         }
  181.  
  182.         ImageDocument(frHndl, true);
  183.     }
  184. }
  185.  
  186.  
  187.  
  188. /*****************************************************************************/
  189.  
  190.  
  191.  
  192. #pragma segment Config
  193. void    DrawPalette(FileRecHndl frHndl)
  194. {
  195.     short        rr, cc, piece;
  196.     Rect        paletteRect;
  197.     Handle        pieceIcon;
  198.  
  199.     PenSize(2, 2);
  200.     for (rr = 1; rr < 6; ++rr) {
  201.         for (cc = -1; cc < 3; cc += 2) {
  202.             paletteRect = PaletteRect();
  203.             paletteRect.left += (rr - 1) * kBoardSqSize;
  204.             if (cc == 1) paletteRect.top += kBoardSqSize;
  205.             paletteRect.right  = paletteRect.left + 32;
  206.             paletteRect.bottom = paletteRect.top + 32;
  207.             piece = rr * cc;
  208.             if (piece == (*frHndl)->doc.palettePiece) piece += 13;
  209.             pieceIcon = GetResource('ICN#', 263 + piece);
  210.             PlotIcon(&paletteRect, pieceIcon);
  211.             InsetRect(&paletteRect, -3, -3);
  212.             FrameRect(&paletteRect);
  213.         }
  214.     }
  215.     PenNormal();
  216. }
  217.  
  218.  
  219.  
  220. /*****************************************************************************/
  221.  
  222.  
  223.  
  224. #pragma segment Config
  225. Rect    PaletteRect(void)
  226. {
  227.     Rect    paletteRect;
  228.  
  229.     paletteRect.top    = 74;
  230.     paletteRect.left   = kBoardWidth + 21;
  231.     paletteRect.right  = paletteRect.left + 5 * kBoardSqSize;
  232.     paletteRect.bottom = paletteRect.top + 2 * kBoardSqSize;
  233.     OffsetRect(&paletteRect, -4096, 0);
  234.  
  235.     return(paletteRect);
  236. }
  237.  
  238.  
  239.  
  240.